package com.homegoods.orderinfodetail.presenter;

import android.databinding.ObservableField;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.bean.AddAddressParams;
import com.homegoods.orderinfodetail.bean.AddressListModel;
import com.homegoods.orderinfodetail.event.EditAddressEvent;
import com.homegoods.orderinfodetail.ui.view.IAddAddressView;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.ToastUtil;

import io.reactivex.functions.Consumer;


/**
 * @author QiQi
 */
public class AddNewAddressPresenter extends BasePresenter<IAddAddressView> {
    private static final String TAG = "AddAddressPresenterTag";

    private boolean isEditMode = false;
    private int index = 0;
    private AddressListModel model;   //del shibo.zheng 19/07/01
    private int postAreaSid;

    public void setPostAreaSid(int sid) {
        this.postAreaSid = sid;
    }

    /**
     * 收货人
     */
    public final ObservableField<String> name = new ObservableField<>();
    /**
     * 手机号码
     */
    public final ObservableField<String> phone = new ObservableField<>();
    /**
     * 所在地区
     */
    public final ObservableField<String> area = new ObservableField<>();
    /**
     * 详细地址
     */
    public final ObservableField<String> address = new ObservableField<>();

    public AddNewAddressPresenter(IAddAddressView view) {
        super(view);
    }

    public void setAddressModel(int index, AddressListModel addressModel) {
        if (addressModel == null) {
            return;
        }
        model = addressModel;
        isEditMode = true;
        this.index = index;

        address.set(addressModel.getDetailAddress());
        phone.set(addressModel.getPhone());
        name.set(addressModel.getName());
        area.set(addressModel.getAreaName());
    }

    /**
     * 保存按钮点击事件
     * @param view
     */
    public void onSaveClick(View view) {
        AddAddressParams params = new AddAddressParams();
        if (TextUtils.isEmpty(address.get())) {
            ToastUtil.showSnackbar(view, "请填写详细地址");
            return;
        } else if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.showSnackbar(view, "请填写手机号");
            return;
        } else if (TextUtils.isEmpty(name.get())) {
            ToastUtil.showSnackbar(view, "请填写收货人姓名");
            return;
        } else if (TextUtils.isEmpty(area.get())) {
            ToastUtil.showSnackbar(view, "请选择收货人所在地区");
            return;
        }

        if (model == null || !isEditMode) {
            model = new AddressListModel();
//            model.setId(0);   //del shibo.zheng
        }
        model.setDetailAddress(address.get());
        model.setPhone(phone.get());
        model.setAreaName(area.get());
        model.setName(name.get());
        model.setMemberId(postAreaSid);
        model.setIsDefault(mView.isCheck());

        params.setAreaId(postAreaSid);
        params.setIsDefault(mView.isCheck());
        params.setDetailAddress(address.get());
        params.setName(name.get());
        params.setPhone(phone.get());
        params.setAreaName(area.get());

        Log.d(TAG, "onSaveClick: model id == " + model.getId());
        if (model != null && model.getId() != 0) {  //此处判断如果model不为空，并且id不为0的时候，为添加收货地址，否则为修改收货地址（临时处理方式）
            params.setId(model.getId());
            OrderInfoApiUtils.updateAddress(params, s -> {
                //保存地址成功，发送消息到我的收货地址界面，通知界面刷新
                RxBus.getDefault().post(new EditAddressEvent(isEditMode, index, model));
                mView.submit();
            }, throwable -> {});
        } else {        //进入此条件为添加收货地址
            OrderInfoApiUtils.setArea(params, jsonObject -> {
                //保存地址成功，发送消息到我的收货地址界面，通知界面刷新
                RxBus.getDefault().post(new EditAddressEvent(isEditMode, index, model));
                mView.submit();
            }, throwable -> {});
        }
    }
}